package com.shen.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shen.server.pojo.Employee;
import com.shen.server.mapper.EmployeeMapper;
import com.shen.server.pojo.ResPageBean;
import com.shen.server.pojo.RespBean;
import com.shen.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.Format;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HMF
 * @since 2022-04-10
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有员（分页）
    * @date: 2022/4/21
    * @param: currentPage
    * @param: size
    * @param: employee
    * @param: beginDateScope
    * @return
    */

    @Override
    public ResPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage, size);
        // IPage是国人开发的一个插件，用来做分页，可以点击去看源码（发现里面是中文的注释，因为是国人开发的）
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        ResPageBean respPageBean = new ResPageBean(employeeByPage.getTotal(), employeeByPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取员工最大工号
    * @date: 2022/4/21
    * @return
    */
    @Override
    public RespBean maxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        // 这里使用String.format格式化防止出现乱码的情况
        return RespBean.success(null,String.format("%08d",Integer.parseInt(maps.get(0).get("max(workID)").toString())+1));

    }

    /**
     * 添加员工
     * @date: 2022/4/21
     * @param: employee
     * @return
     */
    @Override
    public RespBean addEmployee(Employee employee) {
     //处理合同期限
        //获取合同开始时间
        LocalDate beginContract = employee.getBeginContract();
        //获取合同结束时间
        LocalDate endContract = employee.getEndContract();
        //计算合同有效时间天数
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        //格式化，保留两位小数
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if (1 == employeeMapper.insert(employee)){
            return RespBean.success("添加成功！");
        }else {
            return RespBean.error("添加失败！");
        }
    }

    /**
     * 更新员工
    * @date: 2022/4/21
    * @param: employee
    * @return
    */
    @Override
    public RespBean updateEmployee(Employee employee) {

        //处理合同期限
        //获取合同开始时间
        LocalDate beginContract = employee.getBeginContract();
        //获取合同结束时间
        LocalDate endContract = employee.getEndContract();
        if ( null != beginContract && null != endContract){
            //计算合同有效时间天数
            long days = beginContract.until(endContract, ChronoUnit.DAYS);
            //格式化，保留两位小数
            DecimalFormat decimalFormat = new DecimalFormat("##.00");
            employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        }else {
            Double contractTerm = employeeMapper.selectById(employee.getId()).getContractTerm();
            employee.setContractTerm(contractTerm);
        }
        if (1 == employeeMapper.updateById(employee)){
            return RespBean.success("更新成功！");
        }else {
            return RespBean.error("更新 失败！");
        }
    }
    /**
     * 查询员工（id为null）时查所有
     * @date: 2022/4/22
     * @param: id
     * @return
     */
    @Override
    public List<Employee> getEmployees(Integer id) {
        return employeeMapper.getEmployees(id);
    }

    /**
     * 获取所以员工账套(分页)
     * @date: 2022/4/24
     * @param: currenPage
     * @param: size
     * @return
     */
    @Override
    public ResPageBean getEmplWithSlary(Integer currenPage, Integer size,Employee employee) {
        //开启分页
        Page<Employee> page = new Page<>(currenPage,size);
        IPage<Employee> employeeIPage =  employeeMapper.getEmplWithSlary(page,employee);
        ResPageBean resPageBean = new ResPageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return resPageBean;
    }
}
